package com.wnhz.work.yz.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.FirstUpdataBean;
import com.wnhz.work.yz.bean.ProjectChild1;
import com.wnhz.work.yz.view.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/12/26.
 */
public class ProjectShopAdapter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private List<ProjectChild1> dataList = new ArrayList<>();
    private int posi = 0;

    public ProjectShopAdapter(Context context, View.OnClickListener listener, List<ProjectChild1> dataList) {
        this.context = context;
        this.listener = listener;
        this.dataList = dataList;
    }


    @Override
    public int getCount() {
        // Toast.makeText(context, dataList.size() + "", Toast.LENGTH_SHORT).show();
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        posi = position;
        final ProjectShopViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.shopping_project_item, null);
            viewHolder = new ProjectShopViewHolder();
            viewHolder.rl_choose = (RelativeLayout) convertView.findViewById(R.id.rl_choose);
            viewHolder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            viewHolder.iv_img = (RoundImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_product = (TextView) convertView.findViewById(R.id.tv_product);
            viewHolder.tv_sku_info = (TextView) convertView.findViewById(R.id.tv_sku_info);
            viewHolder.tv_goods_type = (TextView) convertView.findViewById(R.id.tv_goods_type);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.shopping_number = (TextView) convertView.findViewById(R.id.shopping_number2);
            viewHolder.reduce = (ImageView) convertView.findViewById(R.id.reduce);
            viewHolder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add);
            viewHolder.text_yushou = (TextView) convertView.findViewById(R.id.text_yushou);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ProjectShopViewHolder) convertView.getTag();
        }

        if (dataList.get(position).isSelect()) {
            viewHolder.iv_choose.setImageResource(R.mipmap.ic_choose_li);
        } else {
            viewHolder.iv_choose.setImageResource(R.mipmap.ic_choose_nor);
        }
        if ("1".equals(dataList.get(position).getType())) {
            viewHolder.text_yushou.setVisibility(View.VISIBLE);
            viewHolder.reduce.setVisibility(View.GONE);
            viewHolder.iv_add.setVisibility(View.GONE);
            viewHolder.shopping_number.setText("×" + dataList.get(position).getNums() + "");
            //     viewHolder.shopping_number.setEnabled(false);
        } else {
            viewHolder.shopping_number.setText(dataList.get(position).getNums() + "");
            viewHolder.text_yushou.setVisibility(View.GONE);
            viewHolder.reduce.setVisibility(View.VISIBLE);
            viewHolder.iv_add.setVisibility(View.VISIBLE);
            viewHolder.shopping_number.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    View view = View.inflate(context, R.layout.dialog_edit, null);
                    TextView queding1 = (TextView) view.findViewById(R.id.queding1);
                    TextView quxaio1 = (TextView) view.findViewById(R.id.quxiao1);
                    final EditText shopping_number = (EditText) view.findViewById(R.id.shopping_number);
                    shopping_number.setText(dataList.get(position).getNums());

                    shopping_number.postInvalidate();
                    //切换后将EditText光标置于末尾
                    CharSequence charSequence = shopping_number.getText();
                    if (charSequence instanceof Spannable) {
                        Spannable spanText = (Spannable) charSequence;
                        Selection.setSelection(spanText, charSequence.length());
                    }

                    builder.setView(view);
                    final AlertDialog show = builder.show();
                    Window dialogWindow = show.getWindow();
                    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                    dialogWindow.setGravity(Gravity.CENTER);
                    dialogWindow.setAttributes(lp);
                    queding1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int nums = Integer.parseInt(dataList.get(position).getNums());
                            try {
                                nums = Integer.parseInt(shopping_number.getText().toString());
                            } catch (Exception e) {
                                nums = Integer.parseInt(dataList.get(position).getNums());
                            }
                            if (nums == 0) {
                                nums = 1;
                                Toast.makeText(context, "购买数量不能小于1", Toast.LENGTH_SHORT).show();
                            }

                            dataList.get(position).setNums(nums + "");
                            FirstUpdataBean m = new FirstUpdataBean();
                            m.setPosition(position);
                            m.setNum(nums);
                            EventBus.getDefault().post(m);
                            viewHolder.shopping_number.setText(nums + "");
                            show.dismiss();
                        }
                    });
                    quxaio1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            show.dismiss();
                        }
                    });
                }
            });
        }
        x.image().bind(viewHolder.iv_img, dataList.get(position).getImg());
        viewHolder.tv_goods_name.setText(dataList.get(position).getPackage_name() + "");
        viewHolder.tv_price.setText("¥ " + dataList.get(position).getPackage_money());

        Log.e("-=商品的价格===tv_price==", "" + dataList.get(position).getPackage_money());

        if ("1".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_ju));
        } else if ("0".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.button_main));
        } else if ("2".equals(dataList.get(position).getTypeid())) {
            viewHolder.tv_product.setBackground(context.getResources().getDrawable(R.drawable.buttom_green));
        }
        viewHolder.tv_product.setText(dataList.get(position).getPackage_cate() + "");
        viewHolder.tv_sku_info.setText(dataList.get(position).getPackage_sku_info() + "");
        viewHolder.tv_goods_type.setText("房间号：" + dataList.get(position).getRoom_number());

        viewHolder.rl_choose.setOnClickListener(listener);
        viewHolder.rl_choose.setTag(position);
        viewHolder.rl_choose.setTag(R.id.add111, viewHolder.iv_choose);
        viewHolder.reduce.setOnClickListener(listener);
        viewHolder.reduce.setTag(position);
        viewHolder.reduce.setTag(R.id.add111, viewHolder.shopping_number);

        viewHolder.iv_add.setTag(position);
        viewHolder.iv_add.setOnClickListener(listener);
        viewHolder.iv_add.setTag(R.id.add111, viewHolder.shopping_number);

        return convertView;
    }


    private int numS = 1;

    class ProjectShopViewHolder {
        RelativeLayout rl_choose;
        ImageView iv_choose;
        RoundImageView iv_img;
        TextView tv_goods_name;
        TextView tv_product;
        TextView tv_sku_info;
        TextView tv_goods_type;
        TextView tv_price;
        ImageView reduce;
        TextView shopping_number;
        ImageView iv_add;
        TextView text_yushou;
    }

    protected Dialog dialog;

}
